package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IStrategyCommentService strategyCommentService;


    /**
     * 获取要查看的目标用户信息,返回目标用户页面
     * @param id : 目标用户的id
     * @return
     */
    @GetMapping("/{id}")
    public User getAnotherUser(@PathVariable Long id){
        return userService.get(id);
    }

    /**
     * 查询某个用户的关注数及粉丝数
     * @param id : 用户的id
     * @return
     */
    @GetMapping("/{id}/relation")
    public Map getRelations(@PathVariable Long id){
        return userService.getRelations(id);
    }

    /**
     * 查询两者的关系 followId是否关注了userId
     * @param userId : 被关注者
     * @param followId : 关注者
     * @return
     */
    @GetMapping("/relation")
    public int getTwoUserRelation(Long userId, Long followId){
        return userService.getTwoUserRelation(userId,followId);
    }

    /**
     * 保存两者之间的关系
     * @param userId : 被关注者
     * @param followId : 关注者
     * @return
     */
    @PutMapping("/saveRelation")
    public JsonResult saveRelation(Long userId, Long followId){
        JsonResult jsonResult=new JsonResult();
        try {
            userService.saveRelation(userId,followId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }


    /**
     * 删除两者间的关系
     * @param userId : 被关注者
     * @param followId : 关注者
     * @return
     */
    @DeleteMapping("/deleteRelation")
    public JsonResult deleteRelation( Long userId, Long followId){
        JsonResult jsonResult=new JsonResult();
        try {
            userService.deleteRelation(userId,followId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }

    /**
     * 获取某个用户的点评
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/comments")
    public PageInfo getComments(StrategyCommentQueryObject qo){
       return strategyCommentService.queryCommentsByUserId(qo);
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping
    public JsonResult register(User user){
        JsonResult jsonResult=new JsonResult();
        try {
            userService.register(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 更新某个员工的信息
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    public JsonResult update(User user){
        JsonResult jsonResult=new JsonResult();
        try {
            userService.update(user);
            //更新后用户,页面的sessionStorage用户数据没变,要更新,要把user共享回去
            jsonResult.setUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("更新失败");
        }
        return jsonResult;
    }

    /**
     * 操作某个用户的游记列表
     */
    @GetMapping("/{authorId}/travels")
    public PageInfo travels(@ModelAttribute("qo") TravelQueryObject qo){
        //设置排序 最近更新时间
        qo.setOrderBy("lastUpdateTime DESC");
        return travelService.query(qo);
    }


}
